package com.twu.biblioteca;

import java.util.ArrayList;

public class UserList {
    private ArrayList<UserDetail> userList;
    private String currentUserId;

    UserList() {
        currentUserId = null;
        userList = new ArrayList<UserDetail>();
        initialize();
    }

    private void initialize() {
        userList.add(new UserDetail(new User("rkb", "t@t", "00"), "111-1111", "rkb"));
        userList.add(new UserDetail(new User("zs", "t@t", "00"), "222-2222", "zs"));
    }

    public String getCurrentUserId() {
        return currentUserId;
    }

    public void login() throws Exception {
        Console console = new Console();
        String userId = console.readString("\nEnter id...");
        String password = console.readString("\nEnter password...");
        UserDetail user;

        try {
            user = search(userId);
        } catch (Exception e) {
            throw e;
        }

        boolean validationResult = user.validate(password);

        if(validationResult == true)
            currentUserId = userId;
        else
            throw new Exception();

    }

    public UserDetail search(String userId) throws Exception {
        for(UserDetail user : userList)
            if(user.getUserId().equals(userId))
                return user;
        throw new Exception();
    }
}
